package com.btr.ivle.backgroud;

import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;

import com.btr.ivle.R;
import com.btr.ivle.data.IVLEToken;
import com.btr.ivle.helper.DatabaseModule;
import com.btr.ivle.helper.DatabaseWebcast;
import com.btr.ivle.helper.URLtoString;

public class GetWebcastTask extends AsyncTask<String, Void, Void>{
	
	private Context context;
	
	public GetWebcastTask(Context context){
		this.context = context;
	}

	protected Void doInBackground(String... texts) {
		String CourseCode = texts[0], CourseID = "";
		String ID, MP4, MP3, FileDescription, FileTitle, CreateDate;

		DatabaseModule dbModule = new DatabaseModule(this.context,IVLEToken.getUserID(),null,context.getResources().getInteger(R.integer.DBVersion));
		String table = IVLEToken.getUserID(); 
		String[] columns = new  String[] {dbModule.colID};
		String selection = dbModule.colCode + "=?";
		String[] selectionArgs = new  String[] {CourseCode};
		Cursor c = dbModule.getReadableDatabase().query(table, columns, 
				selection, selectionArgs, null, null, null, null);
		if(c.getCount()==1){
			c.moveToFirst();
			CourseID = c.getString(0);
		}
		c.close();
		dbModule.getReadableDatabase().close();
		dbModule.close();
		
		try{
        	URL url = new URL("https://ivle.nus.edu.sg/api/Lapi.svc/Webcasts?APIKey=" +
        			context.getString(R.string.IVLE_API_KEY) + "&AuthToken=" + IVLEToken.getToken() + "&CourseID="
        				+ CourseID + "&Duration=0&TitleOnly=false");
        	
        	JSONObject jObject = new JSONObject(URLtoString.get(url));
	        JSONArray resultsArrary = jObject.getJSONArray("Results");
	        
	        DatabaseWebcast dbWebcast = new DatabaseWebcast(this.context,IVLEToken.getUserID()+"_Webcast",null,context.getResources().getInteger(R.integer.DBVersion));
	        table = IVLEToken.getUserID() + "_Webcast";
	        ContentValues values;
	        
	        for (int i = 0; i < resultsArrary.length(); i++) {
	        	JSONArray array = resultsArrary.getJSONObject(i).getJSONArray("ItemGroups");
	        	for(int j = 0; j < array.length(); j++){
	        		JSONArray array2 = array.getJSONObject(j).getJSONArray("Files");
	        		for(int k = 0; k < array.length(); k++){
	        			ID = array2.getJSONObject(k).getString("ID").toString();
	        			MP4 = array2.getJSONObject(k).getString("MP4").toString();
						MP3 = array2.getJSONObject(k).getString("MP3").toString();
						FileDescription = array2.getJSONObject(k).getString("FileDescription").toString();
						FileTitle = array2.getJSONObject(k).getString("FileTitle").toString();
						CreateDate = array2.getJSONObject(k).getString("CreateDate").toString();
	        			
	        			values = new  ContentValues();
	    	        	values.put(dbWebcast.ID, ID);
	    	        	values.put(dbWebcast.MP4, MP4);
	    	        	values.put(dbWebcast.MP3, MP3);
	    	        	values.put(dbWebcast.FileDescription, FileDescription);
	    	        	values.put(dbWebcast.FileTitle, FileTitle);
	    	        	values.put(dbWebcast.CreateDate, CreateDate);
	    	        	values.put(dbWebcast.CourseCode, CourseCode);
	    	        	dbWebcast.getReadableDatabase().replace(table, null, values);
	        		}
	        	}
			}
	        dbWebcast.getReadableDatabase().close();
	        dbWebcast.close();
  
        }catch(Exception ex){
	    }
		return null;
	}
	
	@SuppressWarnings("unchecked")
    protected void onPostExecute(Void result) {
		AsyncTaskCompleteListener<String> l = (AsyncTaskCompleteListener<String>) context;
    	l.onAsyncTaskComplete(""); 
    }
}
